package com.qilu.pe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.TodayTest;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.AlertUtil;
import com.qilu.pe.support.util.PeUtil;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.SizeUtils;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AlertDialog;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class TodayTestActivity extends Base2Activity {
    RecyclerView rcv_list;// 今日检验列表
    TextView empty_view;

    private String patientId;// 传过来的病人id 暂时没有该值
    private TodayTest currTest;// 记录当前操作的检验

    private TodayTestAdapter adapter;
    private List<TodayTest> testList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_today_test;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_list = findViewById(R.id.rcv_list);
        empty_view = findViewById(R.id.empty_view);

        setHeadTitle("今日检验");

        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new TodayTestAdapter(this);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

        alert = new AlertDialog.Builder(this, R.style.Dialog_FS).create();

        hideProgress();
        reqtestOrderList();
        showProgress();
    }

    // 今日检验?
    private Disposable reqtestOrderList() {
        return APIRetrofit.getDefault().reqtestOrderList(Global.HEADER, 1, 999999, patientId, "", "1", "")
                .compose(RxUtil.<BaseResult2<List<TodayTest>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<TodayTest>>>() {
                    @Override
                    public void accept(BaseResult2<List<TodayTest>> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {
                            testList = listBaseResult2.getData();
                            adapter.setDataList(testList);
                        } else {
                            ToastUtils.showShort(listBaseResult2.getMsg());
                        }
                        if (isDataEmpty(testList)) {
                            empty_view.setVisibility(View.VISIBLE);
                        } else {
                            empty_view.setVisibility(View.GONE);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        empty_view.setVisibility(View.VISIBLE);
                    }
                });
    }

    private Disposable reqCancelOrder(String orderId) {
        return APIRetrofit.getDefault().reqCancelOrder(Global.HEADER, orderId)
                .compose(RxUtil.<BaseResult2>subIoObMain())
                .subscribe(new Consumer<BaseResult2>() {
                    @Override
                    public void accept(BaseResult2 baseResult2) throws Exception {
                        hideProgress();
                        if (baseResult2.isSuccess()) {
                            finish();
                        } else {

                        }
                        ToastUtils.showShort(baseResult2.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("取消订单失败");
                    }
                });
    }

    private AlertDialog alert;// 弹窗
    private View alert_view;

    public void showDialog() {// 弹窗
        alert.show();
        alert_view = View.inflate(this, R.layout.layout_alert_com_alert, null);

        TextView tv_title = alert_view.findViewById(R.id.tv_title);
        tv_title.setText("确定取消预约?");

        alert_view.findViewById(R.id.tv_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.dismiss();
            }
        });
        alert_view.findViewById(R.id.tv_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.dismiss();
                if (null != currTest) {
                    hideProgress();
                    reqCancelOrder(currTest.getId());
                    showProgress();
                }
            }
        });

        AlertUtil.setAlertCenterStyle(alert.getWindow(), alert_view, this);
    }

    private class TodayTestAdapter extends ListBaseAdapter<TodayTest> {

        public TodayTestAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_today_test_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {
            CardView cv_item = holder.getView(R.id.cv_item);
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_state = holder.getView(R.id.tv_state);
            TextView tv_patient_name = holder.getView(R.id.tv_patient_name);
            TextView tv_place_time = holder.getView(R.id.tv_place_time);
            TextView tv_order_time = holder.getView(R.id.tv_order_time);
            TextView tv_btn = holder.getView(R.id.tv_btn);

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) cv_item.getLayoutParams();
            if (position == 0) {
                params.topMargin = SizeUtils.dp2px(10);
                params.bottomMargin = SizeUtils.dp2px(5);
            } else if (position == mDataList.size() - 1) {
                params.topMargin = SizeUtils.dp2px(5);
                params.bottomMargin = SizeUtils.dp2px(10);
            } else {
                params.topMargin = SizeUtils.dp2px(5);
                params.bottomMargin = SizeUtils.dp2px(5);
            }

            final TodayTest test = mDataList.get(position);
            tv_name.setText(test.getProject_name());
            final String type = test.getType();
            final String status = PeUtil.getOrderStatus(type);
            tv_state.setText(status);// type 0待付款、1未接单、2运输中、3检验中、4已出报告、5申请退款、6已退款、7订单取消
            tv_patient_name.setText(test.getName());
            tv_place_time.setText(test.getTime());
            tv_order_time.setText(test.getCreated_at());

            if (TextUtils.equals("0", type)) {
                tv_btn.setText("取消预约");
                tv_btn.setVisibility(View.VISIBLE);
            } else if (TextUtils.equals("1", type)) {
                tv_btn.setText("取消预约");
                tv_btn.setVisibility(View.VISIBLE);
            } else if (TextUtils.equals("2", type)) {
                tv_btn.setText("查看详情");
                tv_btn.setVisibility(View.VISIBLE);
            } else if (TextUtils.equals("3", type)) {
                tv_btn.setText("查看详情");
                tv_btn.setVisibility(View.VISIBLE);
            } else if (TextUtils.equals("4", type)) {
                tv_btn.setText("查看详情");
                tv_btn.setVisibility(View.VISIBLE);
            } else if (TextUtils.equals("5", type)) {
                tv_btn.setText("查看详情");
                tv_btn.setVisibility(View.VISIBLE);
            } else if (TextUtils.equals("6", type)) {
                tv_btn.setText("查看详情");
                tv_btn.setVisibility(View.VISIBLE);
            } else if (TextUtils.equals("7", type)) {
                tv_btn.setText("");
                tv_btn.setVisibility(View.GONE);
            } else {
                tv_btn.setText("");
                tv_btn.setVisibility(View.GONE);
            }

            tv_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    currTest = mDataList.get(position);
                    if (TextUtils.equals("0", type) || TextUtils.equals("1", type)) {
                        // 取消预约
                        showDialog();
                    } else if (TextUtils.equals("2", type) || TextUtils.equals("3", type)
                            || TextUtils.equals("4", type) || TextUtils.equals("5", type)
                            || TextUtils.equals("6", type)) {

//                        title = bundle.getString("title");
//                        orderId = bundle.getString("orderId");
//                        orderType = bundle.getString("orderType");
                        // 查看订单详情
                        Bundle bundle = new Bundle();
                        bundle.putString("title", status);
                        bundle.putString("orderId", test.getId());
                        bundle.putString("orderType", type);
                        startActivity(OrderDetailActivity.class, bundle);

                    } else if (TextUtils.equals("7", type)) {

                    } else {

                    }
                }
            });
        }
    }
}